package com.ac.input.util;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.jdbc.core.JdbcTemplate;

import com.caac.core.config.Config;

public class FieldValueBean {
	private static final Log LOG = LogFactory.getLog(Config.class);
	private JdbcTemplate jt;
	public FieldValueBean(){
		this.jt =(JdbcTemplate) Config.getInstance().getBean("jdbcTemplate");
	}
	
	public Map[] fieldList(String inpCode) {
		Map data[] = new Map[1];
		Map reply = new LinkedHashMap();
		String sql = "SELECT COL_TYP||'.'||COL_CODE COL_CODE, '['|| COL_CODE ||']'|| COL_NAME COL_NAME FROM HND_INP_DEF_ITM WHERE INP_CODE='"
				+ inpCode + "' AND COL_NAME IS NOT NULL ORDER BY VIS_NO";
		List fList = jt.queryForList(sql);
		for (int i = 0; i < fList.size(); i++) {
			Map map = (Map) fList.get(i);
			reply.put((String) map.get("COL_CODE"), (String) map
					.get("COL_NAME"));
		}
		data[0] = reply; 
		return data;
	}

	public Map[] valueList(String inpCode, String fCode) {
		Map data[] = new Map[1];
		Map reply = new LinkedHashMap();
		String[] sCodes = fCode.split("\\.");
		String sql = "SELECT TAB_NAME FROM HND_INP_DEF WHERE INP_CODE='"
				+ inpCode + "'";
		String tName = (String) jt.queryForObject(sql, String.class);
		if ("1".equals(sCodes[0])) {
			sql = "SELECT DISTINCT NVL(" + sCodes[1] + ",'-') COL1 FROM "
					+ tName + " where rownum<100";
		} else if ("2".equals(sCodes[0])) {
			sql = "SELECT DISTINCT NVL(to_char(" + sCodes[1]
					+ "),'-') COL1 FROM " + tName + " where rownum<100";
		} else if ("3".equals(sCodes[0])) {
			sql = "SELECT DISTINCT NVL(to_char(" + sCodes[1]
					+ ",'YYYY-MM-DD'),'-') COL1 FROM " + tName
					+ " where rownum<100";
		}
		List vList = jt.queryForList(sql);
		for (int i = 0; i < vList.size(); i++) {
			Map map = (Map) vList.get(i);
			String value = (String) map.get("COL1");
			reply.put(value, value);
		}
		data[0] = reply;
		return data;
	}

	public Map tmpWhereList(String inpCode) {
		Map reply = new LinkedHashMap();
		String sql = "SELECT ID,WR_NAME FROM HND_TMP_WHERE WHERE INP_CODE='"
				+ inpCode + "'";
		List fList = jt.queryForList(sql);
		for (int i = 0; i < fList.size(); i++) {
			Map map = (Map) fList.get(i);
			reply.put((String) map.get("ID"), (String) map.get("WR_NAME"));
		}
		LOG.info(sql);
		return reply;
	}
	
	public String getTmpWhere(String id) {
		String sql = "SELECT WR_CONT FROM HND_TMP_WHERE WHERE ID='"+id+"'";
		String wrCont = (String )jt.queryForObject(sql, String.class);
		return wrCont;
	}
	
	public String saveTmpWhere(String inpCode, String wrName, String wrCont) {
		String sql = "INSERT INTO HND_TMP_WHERE(ID,INP_CODE,WR_NAME,WR_CONT,CRT_DATE,CRT_USER) VALUES(?,?,?,?,SYSDATE,NULL)";
		String id = com.ac.input.util.UUID.randomUUID().toString();
		jt.update(sql, new Object[] {id, inpCode,wrName, wrCont });
		return id;
	}
	
	public void delTmpWhere(String id) {
		String sql = "DELETE FROM HND_TMP_WHERE WHERE ID='"+id+"'";
		jt.execute(sql);
	}

	public Map[] valuesByParam(String tmpId, String param1) {
		Map data[] = new Map[1];
		Map reply = new LinkedHashMap();
		String sql = "SELECT WR_CONT FROM HND_TMP_WHERE WHERE ID='"+ tmpId + "'";
		sql = (String) jt.queryForObject(sql, String.class);
		List vList = jt.queryForList(sql,new Object[]{param1});
		for (int i = 0; i < vList.size(); i++) {
			Map map = (Map) vList.get(i);
			reply.put((String) map.get("CODE"), (String) map.get("NAME"));
		}
		data[0] = reply;
		return data;
	}
	
	public Map[] valuesByParam(String tmpId, String param1,String param2) {
		Map data[] = new Map[1];
		Map reply = new LinkedHashMap();
		String sql = "SELECT WR_CONT FROM HND_TMP_WHERE WHERE ID='"+ tmpId + "'";
		sql = (String) jt.queryForObject(sql, String.class);
		List vList = jt.queryForList(sql,new Object[]{param1,param2});
		for (int i = 0; i < vList.size(); i++) {
			Map map = (Map) vList.get(i);
			reply.put((String) map.get("CODE"), (String) map.get("NAME"));
		}
		data[0] = reply;
		return data;
	}
}
